package com.cn.servlet.news;

import com.cn.Dao.NewsinfoDao;
import com.cn.Dao.UserDao;
import com.cn.base.Util;
import com.cn.entity.NewsInfo;
import com.cn.entity.UserInfo;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

@WebServlet(name = "InsertNewsServlet")
public class InsertNewsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message="";
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            NewsinfoDao newsinfoDao=new NewsinfoDao();
            NewsInfo newsInfo= new NewsInfo();
            List<FileItem> items=upload.parseRequest(request);
            for(FileItem item: items){
                if(item.isFormField()){
                    //文本
                    String divname = item.getFieldName();
                    if(divname.equals("number")){
                        String number=item.getString();
                        System.out.println("number:"+number);
                        newsInfo.setNum(number);
                    }
                    else if(divname.equals("newsname")){
                        String newsname=item.getString();
                        System.out.println("newsname:"+newsname);
                        newsInfo.setName(newsname);
                    }
                    else if(divname.equals("id")){
                        String id=item.getString();
                        System.out.println("id:"+id);
                        newsInfo.setId(Integer.valueOf(id));
                    }
                    else if(divname.equals("pub")){
                        String pub=item.getString();
                        System.out.println("pub:"+pub);
                        newsInfo.setPub(pub);
                    }
                    else if(divname.equals("term")){
                        String term=item.getString("UTF-8");
                        int tmp=Integer.parseInt(term);
                        if(tmp==1){ term="日报";}else if(tmp==2){term="周刊";}else if(tmp==3){term="半月刊";}else{term="月刊";}
                        newsInfo.setTerm(term);
                    }
                    else if(divname.equals("info")){
                        String info=item.getString();
                        System.out.println("info:"+info);
                        newsInfo.setCon(info);
                    }
                    else if(divname.equals("price")){
                        String price=item.getString();
                        System.out.println("price:"+price);
                        newsInfo.setPrice(Float.valueOf(price));
                    }
                }
                else{
                    //文件
                    String imgtypy=item.getName().substring(item.getName().lastIndexOf("."));
                    String fileName = UUID.randomUUID()+imgtypy;
                    System.out.println("filename:"+fileName);
                    String path=System.getProperty("user.dir")+"\\images\\";
                    File file1 =new File(path);
                    //如果文件夹不存在则创建
                    if  (!file1 .exists()  && !file1 .isDirectory())
                    {
                        file1 .mkdir();
                    }
                    File file = new File(path,fileName);
                    item.write(file);
                    path = path.replaceAll("\\\\","/");
                    System.out.println(path+"/"+fileName);
                    newsInfo.setPath(path+"/"+fileName);
                }
            }
            newsinfoDao.Insert(newsInfo);
            message="上传成功";
            request.setAttribute("message",message);
            request.getRequestDispatcher("newspaper_all").forward(request,response);
        } catch (FileUploadException e) {
            e.printStackTrace();
            message="上传失败";
            request.setAttribute("message",message);
            request.getRequestDispatcher("addbooks.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            message="上传失败";
            request.setAttribute("message",message);
            request.getRequestDispatcher("addbooks.jsp").forward(request,response);
        }
        finally {
            request.setAttribute("message", message);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
